<template><div><h2 id="在没有配置的情况下-springboot默认的文件扫描范围怎么划定的" tabindex="-1"><a class="header-anchor" href="#在没有配置的情况下-springboot默认的文件扫描范围怎么划定的"><span>在没有配置的情况下，springboot默认的文件扫描范围怎么划定的</span></a></h2>
<p>springboot在默认情况下扫描SpringBootApplicaiton注解标注的启动类所在的包。在启动类包所在的所有类都会被springboot扫描到</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz</span><span style="color:#ABB2BF">;</span><span style="color:#7F848E;font-style:italic"> // 声明在启动类所在包的外面</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.stereotype.Component</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic">/**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Author haomingze</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Date 2025/3/26 14:24</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @description:</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> */</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">Component</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> Test</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz.springbootdemo01.config</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.beans.factory.annotation.Autowired</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.context.annotation.Configuration</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> xyz.hmz.Test</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic">/**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Author haomingze</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Date 2025/3/26 14:29</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @description:</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> */</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">Configuration</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> TestConfig</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">Autowired</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B"> Test</span><span style="color:#E06C75"> test</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><p>运行程序观测结果可以发现，程序无法启动，因为找不到Test这个Bean</p>
<h2 id="如何包含或排除特定的目录" tabindex="-1"><a class="header-anchor" href="#如何包含或排除特定的目录"><span>如何包含或排除特定的目录</span></a></h2>
<p>可以使用@ComponentScan注解添加一些不会被扫描的包或者类</p>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz.springbootdemo01</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.boot.SpringApplication</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.boot.autoconfigure.SpringBootApplication</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.context.annotation.ComponentScan</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">SpringBootApplication</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">ComponentScan</span><span style="color:#E06C75">(</span></span>
<span class="line"><span style="color:#D19A66">        basePackages</span><span style="color:#56B6C2"> =</span><span style="color:#E06C75"> {</span><span style="color:#98C379">"xyz.hmz"</span><span style="color:#E06C75">}</span></span>
<span class="line"><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> SpringbootDemo01Application</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#C678DD"> void</span><span style="color:#61AFEF"> main</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">String</span><span style="color:#ABB2BF">[] </span><span style="color:#E06C75;font-style:italic">args</span><span style="color:#ABB2BF">)</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#E5C07B">        SpringApplication</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">run</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">SpringbootDemo01Application</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">class</span><span style="color:#ABB2BF">, args);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><p>这样上面Test的Bean就会被扫描上了<br>
排除指定的包同样可以使用@Component注解+正则的方式</p>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz.springbootdemo01</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.boot.SpringApplication</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.boot.autoconfigure.SpringBootApplication</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.context.annotation.ComponentScan</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.context.annotation.FilterType</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">SpringBootApplication</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">ComponentScan</span><span style="color:#E06C75">(</span></span>
<span class="line"><span style="color:#D19A66">        basePackages</span><span style="color:#56B6C2"> =</span><span style="color:#E06C75"> {</span><span style="color:#98C379">"xyz.hmz"</span><span style="color:#E06C75">}</span><span style="color:#ABB2BF">,</span></span>
<span class="line"><span style="color:#D19A66">        excludeFilters</span><span style="color:#56B6C2"> =</span><span style="color:#E06C75"> { </span><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">ComponentScan.Filter</span><span style="color:#E06C75">(</span><span style="color:#D19A66">type</span><span style="color:#56B6C2"> =</span><span style="color:#E5C07B"> FilterType</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">REGEX</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> pattern</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "xyz.hmz.Test"</span><span style="color:#E06C75">) } </span></span>
<span class="line"><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> SpringbootDemo01Application</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#C678DD"> void</span><span style="color:#61AFEF"> main</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">String</span><span style="color:#ABB2BF">[] </span><span style="color:#E06C75;font-style:italic">args</span><span style="color:#ABB2BF">)</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#E5C07B">        SpringApplication</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">run</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">SpringbootDemo01Application</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">class</span><span style="color:#ABB2BF">, args);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><p>这样就又无法启动了，因为Test会被再次排除</p>
<h2 id="mybatis-的-mapper-xml文件、yaml配置文件的扫描和bean的扫描有什么区别" tabindex="-1"><a class="header-anchor" href="#mybatis-的-mapper-xml文件、yaml配置文件的扫描和bean的扫描有什么区别"><span>mybatis 的 mapper.xml文件、yaml配置文件的扫描和Bean的扫描有什么区别</span></a></h2>
<p>mybatis的mapper.xml和.yaml通过io流的形式被扫描进来的，而Bean的扫描通过注解的形式（底层是反射）。</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">Test</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD">  void</span><span style="color:#61AFEF"> testMybatis</span><span style="color:#E06C75"> () throws IOException {</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    // 获取配置文件输入流</span></span>
<span class="line"><span style="color:#E5C07B">    InputStream</span><span style="color:#E06C75"> inputStream </span><span style="color:#56B6C2">=</span><span style="color:#E5C07B"> Resources</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getResourceAsStream</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"mybatis-config.xml"</span><span style="color:#ABB2BF">);</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    // 通过SqlSessionFactoryBuilder的build()方法创建SqlSessionFactory实例</span></span>
<span class="line"><span style="color:#E5C07B">    SqlSessionFactory</span><span style="color:#E06C75"> sqlSessionFactory </span><span style="color:#56B6C2">=</span><span style="color:#C678DD"> new</span><span style="color:#61AFEF"> SqlSessionFactoryBuilder</span><span style="color:#E06C75">()</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">build</span><span style="color:#ABB2BF">(inputStream);</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    // 调用openSession()方法创建SqlSession实例</span></span>
<span class="line"><span style="color:#E5C07B">    SqlSession</span><span style="color:#E06C75"> sqlSession </span><span style="color:#56B6C2">=</span><span style="color:#E5C07B"> sqlSessionFactory</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">openSession</span><span style="color:#ABB2BF">();</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    // 获取UserMapper代理对象</span></span>
<span class="line"><span style="color:#E5C07B">    UserMapper</span><span style="color:#E06C75"> userMapper </span><span style="color:#56B6C2">=</span><span style="color:#E5C07B"> sqlSession</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getMapper</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">UserMapper</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">class</span><span style="color:#ABB2BF">);</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    // 执行Mapper方法，获取执行结果</span></span>
<span class="line"><span style="color:#E5C07B">    List</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E5C07B">UserEntity</span><span style="color:#ABB2BF">></span><span style="color:#E06C75"> userList </span><span style="color:#56B6C2">=</span><span style="color:#E5C07B"> userMapper</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">listAllUser</span><span style="color:#ABB2BF">();</span></span>
<span class="line"><span style="color:#E5C07B">    System</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">out</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">println</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">JSON</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">toJSONString</span><span style="color:#ABB2BF">(userList));</span></span>
<span class="line"><span style="color:#E06C75">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="完整的串联一遍springboot启动时的扫描与bean加载过程-画图" tabindex="-1"><a class="header-anchor" href="#完整的串联一遍springboot启动时的扫描与bean加载过程-画图"><span>完整的串联一遍Springboot启动时的扫描与Bean加载过程，画图</span></a></h2>
<p><img src="@source/notes/每周一学/4.第四周学习/3-1.png" alt=""></p>
<blockquote>
<p>参考：<br>
注解底层原理 https://blog.csdn.net/KingBoyWorld/article/details/105337011<br>
mybatis解析与加载原理https://blog.csdn.net/Howinfun/article/details/106383895</p>
</blockquote>
</div></template>


